// pages/centerMsg/centerMsg.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userMsg:[],
    username:'',
    useremail:'',
    userphone:'',
  },
  onLoad:function(){
    //获取用户id发送请求
    var userid = wx.getStorageSync("OoOoUserid");
    var data = {
      userid:userid,
    }
    var that = this;
    //获取个人资料信息
    wx.request({
      url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/index_lwx',
      dataType:'json',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
          that.setData({
            //第一个data为固定用法
            userMsg: res.data
          })
        },
        fail:function(err){
        console.log(err)
        }
    });
  },
  //缓存性别
  swiperChange:function(e){
    var newsex = e.detail.value;
    wx.setStorageSync('sex',newsex);
 },
 //保存修改
  saveMsgBtn:function(e){
    var that = this;
    if(e.detail.value.username == ''){
      wx.showToast({
        title: '用户名不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(e.detail.value == ''){
      wx.showToast({
        title: '请设置性别',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(e.detail.value.userphone == ''){
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(e.detail.value.useremail == ''){
      wx.showToast({
        title: '邮箱不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var username = e.detail.value.username;
    var userphone = e.detail.value.userphone;
    var useremail = e.detail.value.useremail;
    var usersex = wx.getStorageSync('sex');
    var userid = wx.getStorageSync("OoOoUserid");
    
    var data = {
      userid : userid,
      username : username,
      usersex : usersex,
      userphone : userphone,
      useremail : useremail,
    }

    wx.request({
      url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/updateUserMsg',
      dataType:'json',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
          if(res.data){
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })

            that.onLoad()
            return;
          }
        },
        fail:function(err){
        console.log(err)
        }
    });
  },
  
})